﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Security.Permissions;
using Function;
using UI.UserControl.Base;

namespace UI.UserControl.UI
{
    public partial class RewritePager : RewritePagerBase
    {
        /// <summary>
        /// 分页事件
        /// </summary>
        public event EventHandler BindData;

        protected override void OnInit(EventArgs e)
        {
            var pagesize = ConfigurationManager.AppSettings["pagesize"];
            if (pagesize.IsNotEmpty() && pagesize.IsNumber())
            {
                if (pagesize.ToInt32() > 0 && pagesize.ToInt32() <= 100)
                    PageSize = pagesize.ToInt32();
            }
            base.OnInit(e);
        }

        /// <summary>
        /// 初始化属性
        /// </summary>
        private void BuildPagerHtml(int RecordCount, int PageIndex, int PageSize)
        {
            //document.getElementsById(\"pageIndex\").value
            int CurPage = PageIndex;
            string[] urlKeys = Request.QueryString.AllKeys;
            string newUrl = "javascript:document.getElementById(\"Pager_pageIndex\").value = \"{0}\";document.getElementById(\"Pager_pageIndexChangedBtn\").click();";




            int Step = 4;//偏移量
            int LeftNum = 0;//做界限
            int RightNum = 0;//右界限
            int PageCount = (int)Math.Ceiling((double)(RecordCount) / PageSize);

            if (CurPage - Step < 1)
            {
                LeftNum = 1;
            }
            else
            {
                LeftNum = CurPage - Step;
            }
            if (CurPage + Step > PageCount)
            {
                RightNum = PageCount;
            }
            else
            {
                RightNum = CurPage + Step;
            }
            PagerHtml = "<div class='pagination pagination-right alternate'><ul>";
            if (CurPage <= 1)
            {
                PagerHtml += " <li class='disabled'><a href='javascript:void(0)'>首页</a></li>";
            }
            else
            {

                PagerHtml += " <li><a href='" + string.Format(newUrl, 1) + "'>首页</a></li>";
            }
            if (CurPage > 1)
            {
                PagerHtml += "<li><a href='" + string.Format(newUrl, CurPage - 1) + "'>上一页</a></li>";
            }
            else
            {
                PagerHtml += "<li class='disabled'><a href='javascript:void(0)'>上一页</a></li>";
            }

            for (int i = LeftNum; i <= RightNum; i++)
            {
                if (i == CurPage)
                {
                    PagerHtml += "<li class='active'><a href='javascript:void(0)'>" + i + "</a> </li>";
                }
                else
                {
                    PagerHtml += "<li><a href='" + string.Format(newUrl, i) + "'>" + i + "</a></li>";
                }
            }

            //末页判断
            //如果当前页是最后一页 则末页和下一页失效
            //如果页码总共只有一页 则末页和下一页失效
            if (CurPage == PageCount || PageCount <= 1)
            {
                PagerHtml += "<li class='disabled'><a href='javascript:void(0)'>下一页</a></li>";
            }
            else
            {
                PagerHtml += "<li><a href='" + string.Format(newUrl, (CurPage + 1)) + "'>下一页</a></li>";
            }

            if (CurPage == PageCount || PageCount <= 1)
            {
                PagerHtml += " <li class='disabled'><a href='javascript:void(0)'>末页</a></li>";
            }
            else
            {
                PagerHtml += " <li><a href='" + string.Format(newUrl, PageCount.ToString()) + "'>末页</a></li>";
            }
            PagerHtml += "</ul></div>";
        }

        /// <summary>
        /// 记录条数
        /// </summary>
        public int _RecordCount;
        public int RecordCount { get { return _RecordCount; } set { DataChanged(value); } }

        public void DataChanged(int recordCount)
        {
            _RecordCount = recordCount;
            ViewState["RecordCount"] = recordCount;
            BuildPagerHtml(RecordCount, CurrentPageIndex, PageSize);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var currentPageIndex = 0;
            if (Session["newQurey"] == "1")
            {
                pageIndex.Text = "1";
                Session["newQurey"] = null;
            }
            if (pageIndex.Text.IsNotEmpty() && pageIndex.Text.IsNumber())
            {

                bool isChanged = false;
                currentPageIndex = (ViewState["PageIndex"] ?? 0).ToInt32();
                isChanged = pageIndex.Text.ToInt32() != currentPageIndex;
                currentPageIndex = pageIndex.Text.ToInt32();
                CurrentPageIndex = currentPageIndex;
                if (isChanged)
                {
                    ViewState["PageIndex"] = currentPageIndex;
                    BindData(sender, e);
                }
            }
            BuildPagerHtml(ViewState["RecordCount"].ToInt32(), CurrentPageIndex, PageSize);
        }

        protected void pageIndexChangedBtn_Click(object sender, EventArgs e)
        {

        }
    }
}